<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.*" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.zhlx.duss.utils.PropUtil" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%--调用WMS的查询库存量接口--%>
<%
    JSONObject jsonData = new JSONObject();
    String MATNR = Util.null2String(request.getParameter("wlh"));
    String KZCODE = Util.null2String(request.getParameter("kcgzh"));
    String CHARG = Util.null2String(request.getParameter("pch"));
    String WERKS = Util.null2String(request.getParameter("gc"));
    String LGORT = Util.null2String(request.getParameter("kcdd"));
    String STATUS = Util.null2String(request.getParameter("zt"));
    String code = "DEFEAT";
    String message = "查询失败";
    JSONArray dataList = new JSONArray();
    try {
        if ("".equals(CHARG)) {
            JSONObject paramsJson = new JSONObject();
            paramsJson.put("MATNR", MATNR);//物料号
            paramsJson.put("KZCODE", KZCODE);//库存跟踪号
            paramsJson.put("CHARG", CHARG);//批次号
            paramsJson.put("WERKS", WERKS);//工厂
            paramsJson.put("LGORT", LGORT);//库存地点
            paramsJson.put("STATUS", STATUS);//状态
            JSONObject resultJson = sendOKHttpRequest(paramsJson);
            String status = Util.null2String(resultJson.getString("code"));
            if ("success".equals(status)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String msgText = outputJson.getString("MSG_TEXT");
                if ("S".equalsIgnoreCase(msgType)) {
                    String dataReturnStr = outputJson.getString("DATA");
                    dataList = JSONArray.parseArray(dataReturnStr);
                    jsonData.put("dataList", dataList);
                    code = "SUCCESS";
                    message = msgText;
                }
            }
        }else {
            String arr[] = CHARG.split(",");
            for (String pch : arr) {
                JSONObject paramsJson = new JSONObject();
                paramsJson.put("MATNR", MATNR);//物料号
                paramsJson.put("KZCODE", KZCODE);//库存跟踪号
                paramsJson.put("CHARG", pch);//批次号
                paramsJson.put("WERKS", WERKS);//工厂
                paramsJson.put("LGORT", LGORT);//库存地点
                paramsJson.put("STATUS", STATUS);//状态
                JSONObject resultJson = sendOKHttpRequest(paramsJson);
                String status = Util.null2String(resultJson.getString("code"));
                if ("success".equals(status)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    String msgText = outputJson.getString("MSG_TEXT");
                    if ("S".equalsIgnoreCase(msgType)) {
                        String dataReturnStr = outputJson.getString("DATA");
                        JSONArray reustlList = JSONArray.parseArray(dataReturnStr);
                        dataList.addAll(reustlList);
                        code = "SUCCESS";
                        message = msgText;
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        code = "EXCEPTION";
        message = "执行异常";
    }
    jsonData.put("code", code);
    jsonData.put("dataList", dataList);
    jsonData.put("message", message);
    out.print(jsonData.toJSONString());

%>
<%!
    private static final JSONObject propVal = PropUtil.getPropVal("zhlxwms");
    private static final String URL = propVal.getString("url")+"/wms-api/oaCtl/query";
    private static final String APPKEY = propVal.getString("username");
    private static final String TOKEN = propVal.getString("password");

    public static JSONObject sendOKHttpRequest(JSONObject params) {
        JSONObject resultJson = new JSONObject();
        String code = "fail";
        String message = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params.toJSONString());
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .addHeader("appId", APPKEY)
                    .addHeader("token", TOKEN)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                resultJson = JSONObject.parseObject(result);
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("message", message);
        return resultJson;
    }


%>



